﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Text.RegularExpressions;
namespace OCRUI.OCRBatchManager
{
    public class CharBounds : IBoundedText
    {

        /// <summary>
        /// Matches Tesseract box text data of the form
        /// R[52]->[52](1103,178)->(1181,104) into the groups
        /// char, xStart, yStart, xEnd, yEnd
        /// </summary>
        public const string RegExBoxCharLine = @"(?<char>.{1})\[.{1,10}\]\((?<xStart>[0-9]{1,5}),(?<yStart>[0-9]{1,5})\)->\((?<xEnd>[0-9]{1,5}),(?<yEnd>[0-9]{1,5})\)";
        /// <summary>
        /// Matches the tesseract NewLine
        /// </sumary>
        public const string RegExNL = "<nl>";
        public char Char { get; set; }
    
        public int BoundTextPosition { get; set; }
        public void Parse(string line)
        {
            
            Regex regexBoxCharLine = new Regex(RegExBoxCharLine);
            Regex regexNewLine = new Regex(RegExNL);
            Match mNewLine = regexNewLine.Match(line);
            Match mBoxLine = regexBoxCharLine.Match(line);

            if (mBoxLine.Success)
            {
                if (mBoxLine.Groups["char"].Success)
                {
                    this.Char = Convert.ToChar(mBoxLine.Captures[0].Value.Substring(0,1));
                }
                if (mBoxLine.Groups["xStart"].Success && mBoxLine.Groups["yStart"].Success && mBoxLine.Groups["xEnd"].Success && mBoxLine.Groups["yEnd"].Success)
                {
                    
                    short xL,xR, yT, yB;
                    if(Int16.TryParse(mBoxLine.Groups["xStart"].Value,out xL) && 
                        Int16.TryParse(mBoxLine.Groups["yStart"].Value,out yT) &&
                        Int16.TryParse(mBoxLine.Groups["xEnd"].Value,out xR) &&
                        Int16.TryParse(mBoxLine.Groups["yEnd"].Value, out yB))
                    {
                        this.Bounds = new Rectangle(xL, yT, xR - xL, yB - yT);
                    }
                }
            }
            else if (mBoxLine.Success)
            {
                //cb.Char = 13;
                //cb.Bounds = null;
            }
            
        }
        public override string ToString()
        {
            return this.Char.ToString();
        }

        #region IBoundedText Members

        public Rectangle Bounds
        {
            get { throw new NotImplementedException(); }
            set { }
        }

        public string Parse(List<string> text)
        {
            throw new NotImplementedException();
        }

        public int BoundTextStartPos
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int BoundTextEndPos
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
    
}
